// 定义组件
define(function (require,exports,module) {

    var rem=require('charity/js/config/rem'),//rem字体
        login = {
            // 组件
            components : {
                'v-loading':require('charity/js/components/loading.js'),  // 加载中组件
                'v-alert':require('charity/js/components/alert.js')  // 加载中组件
            },
            data : {
                name:'',
                no:'',
                captcha:'',
                yzmSrc:global.cxt+'/charity/login/captcha?'+ Math.random(),
                loginUrl:global.cxt+'/charity/login/validate',
                showLoading:true//加载中
            },
            // 事件
            methods : {
                // 切换验证码图片
                changeYzm:function () {
                    this.yzmSrc=global.cxt+'/charity/login/captcha?'+ Math.random();
                },
                // 登录
                login:function () {
                    var that = this;
                    // 输入姓名
                    if(!that.name){
                        that.$messagebox({title:'温馨提示',message:'请输入姓名！',showCancelButton: true})
                        return;
                    }
                    // 输入工号
                    if(!that.no){
                        that.$messagebox({title:'温馨提示',message:'请输入工号！',showCancelButton: true})
                        return;
                    }
                    // 输入验证码
                    if(!that.captcha){
                        that.$messagebox({title:'温馨提示',message:'请输入验证码！',showCancelButton: true})
                        return;
                    }
                    // 显示loading
                    that.$indicator.open({
                        spinnerType: 'fading-circle'
                    });
                    // 请求登录
                    $.ajax({
                        url:that.loginUrl,
                        type:'post',
                        dataType:'json',
                        data:{
                        	name: that.name,
                        	no: that.no,
                        	captcha: that.captcha
                        },
                        success:function (res) {
                            // 关闭loading
                            that.$indicator.close();
                            // 登录成功
                            if(res.code=='0'){
                                that.$store.commit('LOGIN',{'account': that.no});//缓存登录信息
                                that.$router.push({path:'list'})//跳转列表页
                            }else{
                                // 错误提示
                            	that.$messagebox({title:'温馨提示',message:res.msg,showCancelButton: true})
                            }
                        }
                    })
                }

            }
        };
    
	var html = require('charity/templates/login.tpl'),
	    index = Vue.extend({
	      	template:html,
		    data:function(){
				return login.data
		    },
		    props:[],
	        components:login.components,
	        beforeCreate:function(){
	        	require('charity/css/login.css');
                require('charity/css/components/e-ui-css.css');
                require('charity/js/lib/mint-ui');
	        },
		    // 实例被创建之后被调用
		    created:function(){
	            // 此处loading初始数据
            },
			computed:{},
            mounted:function(){
                this.showLoading = false;
            },
		    methods:login.methods
		});
	module.exports = index;

});

